package com.action.music;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.action.music.ManageMusic;
import com.beans.music.MusicInfo;
import com.business.music.MusicInfoBus;
import com.config.SystemGlobals;
import com.jspsmart.upload.SmartUpload;
import com.util.MD5;
import com.util.TextTools;

public class ManageMusic extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String contentType = SystemGlobals.getValue("contentType");
		PrintWriter out = response.getWriter();
		response.setContentType(contentType);
		Log log = LogFactory.getLog(ManageMusic.class);

		if (request.getParameter("deleted") != null) {

			String[] musicNos = request.getParameterValues("deleted");
			MusicInfoBus lib = new MusicInfoBus();
			for (int i = 0; i < musicNos.length; i++) {
				lib.del(musicNos[i]);
			}
			out
					.println("<script>alert(\"删除音乐成功\");window.location=\"admin/blog/musiclist.jsp\";</script>");
			out.close();
		} else {
			SmartUpload SmartUpload = new SmartUpload();
			SmartUpload.initialize(getServletConfig(), request, response);
			SmartUpload.setTotalMaxFileSize(0x4c4b40L);
			try {
				SmartUpload.upload();
			} catch (Exception ex) {
				log.error("smartUpload upload():" + ex.getMessage());
			}

			String action = SmartUpload.getRequest().getParameter("action");
			if (action != null && action.equals("edit")) {
				String musicUrl = "";
				String lrcUrl = "";
				String startpath = SystemGlobals.getValue("file_upload_path");
				startpath = startpath + "/music";
				String filepath = "";
				try {
					for (int i = 0; i < SmartUpload.getFiles().getCount(); i++) {
						com.jspsmart.upload.File myfile = SmartUpload
								.getFiles().getFile(i);
						String imagefile = myfile.getFileName();
						if (!myfile.isMissing()) {
							MD5 md5 = new MD5();
							String md5str = md5.getMD5ofStr(myfile
									.getContentString());
							imagefile = md5str + "." + myfile.getFileExt();
							myfile.saveAs(startpath
									+ TextTools.mkHashDir(startpath, md5str)
									+ "/" + imagefile);
							filepath = "/upload/music"
									+ TextTools.mkHashDir(startpath, md5str)
									+ "/" + imagefile;
							if (i == 0) {
								musicUrl = filepath;
							}
							if (i == 1) {
								lrcUrl = filepath;
							}
						}
					}
				} catch (Exception se) {
					log.error("the upload File" + filepath + " is Failed "
							+ se.toString());
				}

				String musicName = SmartUpload.getRequest().getParameter(
						"musicname");
				String musicMemo = SmartUpload.getRequest().getParameter(
						"musicmemo");
				String username = SmartUpload.getRequest().getParameter(
						"username");
				String musictypeId = SmartUpload.getRequest().getParameter(
						"musictypeid");
				String isShow = SmartUpload.getRequest().getParameter("isshow");
				String viewCount = SmartUpload.getRequest().getParameter(
						"viewcount");
				String singer = SmartUpload.getRequest().getParameter("singer");
				String className = SmartUpload.getRequest().getParameter(
						"classname");

				if (isShow == null) {
					isShow = "0";
				}
				MusicInfo mi = new MusicInfo();
				mi.setUsername(username);
				mi.setIsShow(isShow);
				mi.setMusicMemo(musicMemo);
				mi.setMusictypeId(musictypeId);
				mi.setMusicName(musicName);
				mi.setViewCount(viewCount);
				mi.setSinger(singer);
				mi.setClassName(className);
				MusicInfoBus mib = new MusicInfoBus();
				String musicNo = SmartUpload.getRequest().getParameter("no");
				String url = SmartUpload.getRequest().getParameter("musicurl");
				String url2 = SmartUpload.getRequest().getParameter("lrcurl");
				if (musicUrl.equals("")) {
					musicUrl = url;
				}
				if (lrcUrl.equals("")) {
					lrcUrl = url2;
				}
				mi.setMusicUrl(musicUrl);
				mi.setLrcUrl(lrcUrl);
				mib.update(mi, musicNo);
				out
						.println("<script>alert(\"编辑音乐信息成功\");window.location=\"admin/blog/musiclist.jsp\";</script>");
				out.close();
			}
		}
	}

}
